package com.ffcs.cn.action.desktopcontrol.smComboBoxDataset;

import com.supermap.data.DatasetType;
import com.supermap.data.Datasource;
import com.supermap.data.Enum;
import com.supermap.data.PixelFormat;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDataset;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDatasource;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.DatasetTypeUtilities;
import com.supermap.desktop.core.utilties.DefaultValues;
import com.supermap.desktop.core.utilties.PixelFormatUtilities;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;


public class DialogSmComboBoxDataset extends SmDialog {
	private SmComboBoxDataset comboBoxDataset;

	private JLabel labelDatasource;
	private SmComboBoxDatasource comboBoxDatasource;

	private JCheckBox checkBoxShowNullValue;

	private JCheckBox checkBoxShowMosaicChild;

	private JCheckBox checkBoxSupportedNetworkChildDataset;

	private JLabel labelSupportedDatasetTypes;
	private JList<String> listSupportedDatasetTypes;

	private JLabel labelPixelFormats;
	private JList<String> listPixelFormats;


	public DialogSmComboBoxDataset() {
		setTitle("SmComboBoxDataset 示例");
		setSize(560,500);
		initComponents();
		initLayout();
		initListener();
	}

	private void initListener() {
		//设置是否显示空值
		checkBoxShowNullValue.addActionListener(e -> comboBoxDataset.setShowNullValue(checkBoxShowNullValue.isSelected()));
		//设置是否显示镶嵌数据集子数据集
		checkBoxShowMosaicChild.addActionListener(e -> comboBoxDataset.setShowMosaicChild(checkBoxShowMosaicChild.isSelected()));
		//设置是否显示网络数据集的子数据集
		checkBoxSupportedNetworkChildDataset.addActionListener(e ->
				comboBoxDataset.setSupportedNetworkChildDataset(checkBoxSupportedNetworkChildDataset.isSelected()));
		//设置支持的数据集类型
		listSupportedDatasetTypes.addListSelectionListener(e -> {
			List<String> list = listSupportedDatasetTypes.getSelectedValuesList();
			if (list == null || list.isEmpty()){
				return;
			}
			DatasetType[] datasetTypes = list.stream().map(DatasetTypeUtilities::fromString).toArray(DatasetType[]::new);
			comboBoxDataset.setSupportedDatasetTypes(datasetTypes);
		});

		//设置支持的像素格式
		listPixelFormats.addListSelectionListener(e -> {
			List<String> list = listPixelFormats.getSelectedValuesList();
			if (list == null || list.isEmpty()){
				return;
			}
			PixelFormat[] pixelFormats = list.stream().map(PixelFormatUtilities::fromString).toArray(PixelFormat[]::new);
			comboBoxDataset.setPixelFormats(pixelFormats);
		});

		//设置数据源
		comboBoxDatasource.addItemListener(e -> {
			Datasource datasource = comboBoxDatasource.getSelectedDatasource();
			Optional.ofNullable(datasource).ifPresent(comboBoxDataset::setDatasource);
		});
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));

		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private JPanel getRightPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控制面板"));

		panel.add(labelDatasource, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxDatasource, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,0).setFill(GridBagConstraints.HORIZONTAL));

		panel.add(checkBoxShowNullValue, new GridBagConstraintsHelper(0, 1, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(checkBoxShowMosaicChild, new GridBagConstraintsHelper(0, 2, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(checkBoxSupportedNetworkChildDataset, new GridBagConstraintsHelper(0, 3, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));


		panel.add(labelSupportedDatasetTypes, new GridBagConstraintsHelper(0, 4, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));
		JScrollPane scrollPane = new JScrollPane(listSupportedDatasetTypes);
		scrollPane.setPreferredSize(new Dimension(100,100));
		panel.add(scrollPane, new GridBagConstraintsHelper(0, 5, 2, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.BOTH));

		panel.add(labelPixelFormats, new GridBagConstraintsHelper(0, 6, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));
		JScrollPane pane = new JScrollPane(listPixelFormats);
		pane.setPreferredSize(new Dimension(100,100));
		panel.add(pane, new GridBagConstraintsHelper(0, 7, 2, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.BOTH));
		return panel;
	}

	private JPanel getLeftPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控件"));
		comboBoxDataset.setPreferredSize(new Dimension(100,DefaultValues.getDefaultComponentHeight()));
		panel.add(new JLabel("数据集:"), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxDataset, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL));
		return panel;
	}

	private void initComponents() {
		comboBoxDataset = new SmComboBoxDataset();

		labelDatasource = new JLabel("数据源:");
		comboBoxDatasource = new SmComboBoxDatasource();
		if (comboBoxDatasource.getSelectedDatasource() != null){
			comboBoxDataset.setDatasource(comboBoxDatasource.getSelectedDatasource());
		}

		checkBoxShowNullValue = new JCheckBox("显示空值");
		checkBoxShowNullValue.setSelected(comboBoxDataset.isShowNullValue());

		checkBoxShowMosaicChild = new JCheckBox("显示镶嵌数据集的子数据集");
		checkBoxShowMosaicChild.setSelected(comboBoxDataset.isShowMosaicChild());

		checkBoxSupportedNetworkChildDataset = new JCheckBox("显示网络数据集的子数据集");

		labelSupportedDatasetTypes = new JLabel("支持数据集类型：");
		listSupportedDatasetTypes = new JList<>();
		DatasetType[] datasetTypes = DatasetTypeUtilities.getAllDesktopDatasetType();
		String[] types = Arrays.stream(datasetTypes).map(DatasetTypeUtilities::toString).toArray(String[]::new);
		listSupportedDatasetTypes.setListData(types);

		labelPixelFormats = new JLabel("支持的像素格式：");
		listPixelFormats = new JList<>();
		Enum[] enums = PixelFormat.getEnums(PixelFormat.class);
		String[] pixelFormats = Arrays.stream(enums)
				.filter(anEnum -> anEnum != PixelFormat.UNKONOWN)
				.map(anEnum -> PixelFormatUtilities.toString((PixelFormat) anEnum))
				.toArray(String[]::new);
		listPixelFormats.setListData(pixelFormats);
	}
}
